// pages/purchase-order/index.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    hasMore: false,
    params: {
      page: 1,
      pageSize: 10,
      timeType: "0",
      merchant_id: "",
      order_type: "3", // 进货单类型 1：水电工进货单 2：装饰公司经货单 3：经销商或代理商进货单（店铺进货）
    },
    tabs: [
      {
        label: "全部",
        value: "0",
      },
      {
        label: "近3个月",
        value: "3",
      },
      {
        label: "近半年",
        value: "6",
      },
      {
        label: "近1年",
        value: "12",
      },
    ],
  },

  handleTabClick(event) {
    const { value } = event.currentTarget.dataset;

    const { params } = this.data;

    params.timeType = value;
    params.page = 1;

    this.setData({
      params,
      list: [],
    });

    this.getData();
  },

  handleGetMore() {
    const { hasMore, params } = this.data;

    if (hasMore) {
      params.page++;

      this.setData({
        params,
      });

      this.getData();
    }
  },

  getData() {
    const { params } = this.data;

    if (params.timeType === "0") {
      delete params.timeType;
    }

    wx.ajax
      .post("queryInPurchaseOrderPageList.action", params, true)
      .then((res) => {
        // console.log(res);
        if (res.success) {
          const result = res.result;
          const oriList = this.data.list;

          this.setData({
            list: oriList.concat(result.pageList),
            hasMore: result.totalPages > result.page,
          });
        }
      });
  },

  handleToAddPage() {
    const { params } = this.data;

    wx.navigateTo({
      url:
        "/pages/add-purchase-goods-order/index?id=" +
        params.merchant_id +
        "&type=" +
        params.order_type,
    });
  },

  handleToDetailPage(event) {
    const { id } = event.currentTarget.dataset;

    wx.navigateTo({
      url: "/pages/purchase-order-detail/index?id=" + id,
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.type && options.id) {
      const { params } = this.data;

      params.order_type = options.type;
      params.merchant_id = options.id;

      this.setData({
        params,
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const { params } = this.data;

    params.page = 1;
    params.timeType = "0";

    this.setData({
      list: [],
      params,
    });

    this.getData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
